package cn.xxstudy.visitshop.util

import android.content.Context
import android.content.res.Resources
import android.util.TypedValue
import android.view.Gravity
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import cn.xxstudy.visitshop.R
import com.bumptech.glide.Glide

/**
 * @author ：DELL on 2021/3/23 .
 * @packages ：cn.xxstudy.visitshop.util .
 * TODO:扩展函数
 */

fun Toolbar.titleCenter() {
    val toolbar = getChildAt(0) as TextView
    toolbar.layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT
    toolbar.gravity = Gravity.CENTER
}

fun Float.dp2Px(): Float {
    return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        this,
        Resources.getSystem().displayMetrics
    )
}

fun ImageView.load(context: Context, url: String): ImageView {
    Glide.with(context)
        .load(url)
        .into(this)
    return this
}

fun SwipeRefreshLayout.configAppearance() {
    this.setColorSchemeResources(R.color.theme_bg)
}

fun RecyclerView.configAppearance(context: Context?) {
    layoutManager = LinearLayoutManager(context)
    addItemDecoration(
        DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
    )
}